﻿Imports System.Configuration
Imports System.Drawing
Imports Microsoft.CSharp
Imports uMESExternalControl.ToleranceInputLib
Imports System.Text
Imports System.IO

Public Class DataAdjust
#Region "处理数据集中带工序图号的方法"
    ''' <summary>
    ''' 处理数据集中带工序图号的方法
    ''' </summary>
    ''' <param name="colIndex">工序名称在DataSet中的列序号</param>
    ''' <param name="sourceSet">需要处理的DataSet</param>
    ''' <returns></returns>
    ''' <remarks>返回的DataSet中增加一列specNameNew 保存处理过的工序名称，界面显示工序名称时用该列的值显示</remarks>
    Public Shared Function FormatDataSetSpecName(ByRef colIndex As Integer, ByRef sourceSet As DataSet) As DataSet
        If sourceSet Is Nothing Then
            FormatDataSetSpecName = sourceSet
            Exit Function
        End If

        If sourceSet.Tables.Count = 0 Then
            FormatDataSetSpecName = sourceSet
            Exit Function
        End If

        If sourceSet.Tables(0).Rows.Count = 0 Then
            FormatDataSetSpecName = sourceSet
            Exit Function
        End If

        Dim tmpSet As DataSet = sourceSet.Copy()

        tmpSet.Tables(0).Columns.Add("specNameNew")


        For Each row As DataRow In tmpSet.Tables(0).Rows

            'If row("containername").ToString().Trim() = "9728040704/12-04-1" Then
            '    Dim aa As String = ""
            'End If

            Dim strTmp = GetSpecNameWithOutProductNo(row(colIndex).ToString().Trim())
            row.BeginEdit()
            row("specNameNew") = strTmp
            row.EndEdit()
        Next

        tmpSet.AcceptChanges()

        FormatDataSetSpecName = tmpSet
    End Function

    Public Shared Function FormatDataSetSpecName(ByRef colIndex As Integer, ByRef sourceTable As DataTable) As DataTable
        If sourceTable Is Nothing Then
            FormatDataSetSpecName = sourceTable
            Exit Function
        End If

        If sourceTable.Rows.Count = 0 Then
            FormatDataSetSpecName = sourceTable
            Exit Function
        End If

        Dim tmpTable As DataTable = sourceTable.Copy()

        tmpTable.Columns.Add("specNameNew")


        For Each row As DataRow In tmpTable.Rows
            Dim strTmp = GetSpecNameWithOutProductNo(row(colIndex).ToString().Trim())
            row.BeginEdit()
            row("specNameNew") = strTmp
            row.EndEdit()
        Next

        tmpTable.AcceptChanges()

        FormatDataSetSpecName = tmpTable
    End Function

    Public Shared Function FormatDataSetSpecNameByColumName(ByRef strColumName As String, ByRef sourceTable As DataTable) As DataTable
        If sourceTable Is Nothing Then
            FormatDataSetSpecNameByColumName = sourceTable
            Exit Function
        End If

        If sourceTable.Rows.Count = 0 Then
            FormatDataSetSpecNameByColumName = sourceTable
            Exit Function
        End If

        Dim tmpTable As DataTable = sourceTable.Copy()

        tmpTable.Columns.Add("specNameNew")


        For Each row As DataRow In tmpTable.Rows

            For i As Integer = 0 To tmpTable.Columns.Count - 1
                If tmpTable.Columns(i).ColumnName.ToLower().Trim() = strColumName.ToLower.Trim() Then
                    Dim strTmp = GetSpecNameWithOutProductNo(row(strColumName).ToString().Trim())
                    row.BeginEdit()
                    row("specNameNew") = strTmp
                    row.EndEdit()
                End If
            Next
        Next
        tmpTable.AcceptChanges()
        FormatDataSetSpecNameByColumName = tmpTable
    End Function


    Public Shared Function FormatDataSetSpecNameByTwoColumName(ByRef strColumName1 As String, ByRef strColumName2 As String, ByRef sourceTable As DataTable) As DataTable
        If sourceTable Is Nothing Then
            FormatDataSetSpecNameByTwoColumName = sourceTable
            Exit Function
        End If

        If sourceTable.Rows.Count = 0 Then
            FormatDataSetSpecNameByTwoColumName = sourceTable
            Exit Function
        End If

        Dim tmpTable As DataTable = sourceTable.Copy()

        tmpTable.Columns.Add("specNameNew")
        tmpTable.Columns.Add("specNameNew2")

        For Each row As DataRow In tmpTable.Rows

            For i As Integer = 0 To tmpTable.Columns.Count - 1
                If tmpTable.Columns(i).ColumnName.ToLower().Trim() = strColumName1.ToLower.Trim() Then
                    Dim strTmp = GetSpecNameWithOutProductNo(row(strColumName1).ToString().Trim())
                    row.BeginEdit()
                    row("specNameNew") = strTmp
                    row.EndEdit()
                End If
                If tmpTable.Columns(i).ColumnName.ToLower().Trim() = strColumName2.ToLower.Trim() Then
                    Dim strTmp = GetSpecNameWithOutProductNo(row(strColumName2).ToString().Trim())
                    row.BeginEdit()
                    row("specNameNew2") = strTmp
                    row.EndEdit()
                End If

            Next
        Next
        tmpTable.AcceptChanges()
        FormatDataSetSpecNameByTwoColumName = tmpTable
    End Function


    Public Shared Function FormatDataSetSpecName(ByRef colIndex As Integer, ByRef colIndex2 As Integer, ByRef sourceTable As DataTable) As DataTable
        If sourceTable Is Nothing Then
            FormatDataSetSpecName = sourceTable
            Exit Function
        End If

        If sourceTable.Rows.Count = 0 Then
            FormatDataSetSpecName = sourceTable
            Exit Function
        End If

        Dim tmpTable As DataTable = sourceTable.Copy()

        tmpTable.Columns.Add("specNameNew")
        tmpTable.Columns.Add("specNameNew2")


        For Each row As DataRow In tmpTable.Rows
            Dim strTmp = GetSpecNameWithOutProductNo(row(colIndex).ToString().Trim())
            row.BeginEdit()
            row("specNameNew") = strTmp
            row.EndEdit()

            strTmp = GetSpecNameWithOutProductNo(row(colIndex2).ToString().Trim())
            row.BeginEdit()
            row("specNameNew2") = strTmp
            row.EndEdit()
        Next

        tmpTable.AcceptChanges()

        FormatDataSetSpecName = tmpTable
    End Function
#End Region

#Region "从SpecName中分离不带件号的SpecName"
    Public Shared Function GetSpecNameWithOutProductNo(ByVal strSpecName As String) As String
        GetSpecNameWithOutProductNo = ""
        Dim arrayS As String() = strSpecName.Split("-")
        Dim intLength As Integer = arrayS.Length
        Dim i As Integer = 0
        Dim strSpecNameWithOutProductNo As String = ""
        If intLength >= 3 Then
            strSpecNameWithOutProductNo = arrayS(intLength - 2) & "-" & arrayS(intLength - 1)
        Else
            strSpecNameWithOutProductNo = strSpecName
        End If
        GetSpecNameWithOutProductNo = strSpecNameWithOutProductNo
    End Function
#End Region

#Region "检测项转化为显示内容"
    ''' <summary>
    ''' add:Wangjh 20221216 检测项内容生成html格式
    ''' 图片为base64图片
    ''' </summary>
    ''' <param name="strPreviewCode"></param> 
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function ParseCode3(ByVal strPreviewCode As String) As String
        Dim strPreviewCodeTemp, strHtmlStripped, strTemp, strCodeTemp As String
        Dim strFileDoc, strFileTemp, strFilePath, strFileName, strHtml As String
        Dim intStartFlag, intEndFlag, intFlag1, intFlag2, intCodeStartFlag, intCodeEndFlag As Integer
        Dim strImageIndex As String
        Dim objBmp As Bitmap
        Dim strLeft, strUp, strDown, strRight As String
        ParseCode3 = ""

        strPreviewCode = strPreviewCode.Trim().Replace("\r\n\t\t", "")
        strPreviewCode = strPreviewCode.Trim().Replace("<P>", "")
        strPreviewCode = strPreviewCode.Trim().Replace("</P>", "")
        'strPreviewCode = strPreviewCode.Replace("\"", "'")

        intStartFlag = strPreviewCode.IndexOf("<Image>")
        intEndFlag = strPreviewCode.IndexOf("</Image>")
        strHtmlStripped = ""
        strHtml = ""

        'strFileTemp = strMapPath + @"\Images\";


        Dim strImagePath As String
        strImagePath = ConfigurationSettings.AppSettings("ImageGetPath")
        strFileTemp = strImagePath

        Dim oDraw As New clsDrawImage
        Dim dtImage As New DataTable

        While (intStartFlag > -1)

            If (intStartFlag > 0) Then

                '将纯文本输出
                strHtml = strPreviewCode.Substring(0, intStartFlag)
                strPreviewCode = strPreviewCode.Substring(intStartFlag)
                ' ftbFinalHtml.Text += strHtml
                ParseCode3 += strHtml
                intStartFlag = strPreviewCode.IndexOf("<Image>")
                intEndFlag = strPreviewCode.IndexOf("</Image>")
                Continue While

            Else

                strHtml = ""
                strPreviewCodeTemp = strPreviewCode.Substring(intStartFlag + 7, intEndFlag - intStartFlag - 7)
                intCodeStartFlag = strPreviewCodeTemp.IndexOf("<&70><+>")
                intCodeEndFlag = strPreviewCodeTemp.IndexOf("<+><&90>")

                If (intCodeStartFlag > -1) Then

                    '代码为行位公差时
                    strCodeTemp = strPreviewCodeTemp.Replace("<&70><+>", "")
                    strCodeTemp = strCodeTemp.Replace("<+><&90>", "")

                    intFlag1 = strCodeTemp.IndexOf("<")
                    intFlag2 = strCodeTemp.IndexOf(">")
                    While (intFlag1 > -1)

                        strTemp = strCodeTemp.Substring(intFlag1, intFlag2 - intFlag1 + 1)
                        strCodeTemp = strCodeTemp.Replace(strTemp, "")
                        intFlag1 = strCodeTemp.IndexOf("<")
                        intFlag2 = strCodeTemp.IndexOf(">")
                    End While

                    strHtmlStripped = strCodeTemp
                    'strPreviewCodeTemp = strPreviewCode;
                    strPreviewCodeTemp = strPreviewCodeTemp.Replace("<Image>", "")
                    strPreviewCodeTemp = strPreviewCodeTemp.Replace("</Image>", "")

                    objBmp = oDraw.DrawShapeTolerance(strPreviewCodeTemp, strHtmlStripped, strFileTemp)

                Else

                    intCodeStartFlag = strPreviewCodeTemp.IndexOf("<T")
                    intCodeEndFlag = strPreviewCodeTemp.IndexOf("!")
                    If (intCodeStartFlag > -1) Then

                        '代码为上下标公差

                        strLeft = strPreviewCodeTemp.Substring(0, intCodeStartFlag)
                        strUp = strPreviewCodeTemp.Substring(intCodeStartFlag + 2, intCodeEndFlag - intCodeStartFlag - 2)
                        strDown = strPreviewCodeTemp.Substring(intCodeEndFlag + 1, strPreviewCodeTemp.Length - intCodeEndFlag - 2)
                        objBmp = oDraw.DrawTolerance(strLeft, strUp, strDown)

                    Else

                        intCodeStartFlag = strPreviewCodeTemp.IndexOf("<H>")
                        intCodeEndFlag = strPreviewCodeTemp.IndexOf("</H>")
                        If (intCodeStartFlag > -1) Then

                            '代码为只有上公差
                            strLeft = strPreviewCodeTemp.Substring(0, intCodeStartFlag)
                            strUp = strPreviewCodeTemp.Substring(intCodeStartFlag + 3, intCodeEndFlag - intCodeStartFlag - 3)
                            strDown = ""
                            objBmp = oDraw.DrawTolerance(strLeft, strUp, strDown)

                        Else

                            intCodeStartFlag = strPreviewCodeTemp.IndexOf("<L>")
                            intCodeEndFlag = strPreviewCodeTemp.IndexOf("</L>")
                            If (intCodeStartFlag > -1) Then

                                '代码为只有下公差
                                strLeft = strPreviewCodeTemp.Substring(0, intCodeStartFlag)
                                strUp = ""
                                strDown = strPreviewCodeTemp.Substring(intCodeStartFlag + 3, intCodeEndFlag - intCodeStartFlag - 3)
                                objBmp = oDraw.DrawTolerance(strLeft, strUp, strDown)

                            Else

                                intCodeStartFlag = strPreviewCodeTemp.IndexOf("<√>")
                                intCodeEndFlag = strPreviewCodeTemp.IndexOf("</√>")
                                If (intCodeStartFlag > -1) Then

                                    '代码为指定加工方法时
                                    strLeft = strPreviewCodeTemp.Substring(intCodeStartFlag + 3, intCodeEndFlag - intCodeStartFlag - 3)

                                    strRight = strPreviewCodeTemp.Substring(intCodeEndFlag + 4)
                                    objBmp = oDraw.DrawRoughness(strLeft, strRight)

                                Else

                                    intCodeStartFlag = strPreviewCodeTemp.IndexOf("<R>")
                                    intCodeEndFlag = strPreviewCodeTemp.IndexOf("</R>")
                                    If (intCodeStartFlag > -1) Then

                                        '代码为去除材料时
                                        strLeft = strPreviewCodeTemp.Substring(intCodeStartFlag + 3, intCodeEndFlag - intCodeStartFlag - 3)

                                        strRight = ""
                                        objBmp = oDraw.DrawRoughness(strLeft, strRight)

                                    Else

                                        intCodeStartFlag = strPreviewCodeTemp.IndexOf("<Q>")
                                        intCodeEndFlag = strPreviewCodeTemp.IndexOf("</Q>")
                                        If (intCodeStartFlag > -1) Then

                                            '   代码为不去除材料时()
                                            strLeft = ""

                                            strRight = ""
                                            objBmp = oDraw.DrawRoughness(strLeft, strRight)

                                        Else

                                            'objBmp = Nothing
                                            objBmp = oDraw.DrawSpecImgOrString(strPreviewCodeTemp, strFileTemp) 'add:Wangjh 20230301,注释上一句
                                        End If
                                    End If
                                End If
                            End If
                        End If

                    End If
                End If

                '保存
                If IsNothing(objBmp) = False Then

                    Using ms As MemoryStream = New MemoryStream()
                        objBmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png)

                        strHtml = "<img src=""" + "data:image/png;base64," + Convert.ToBase64String(ms.ToArray()) + """>"

                        ParseCode3 += strHtml
                    End Using
                  
                End If

            End If
            If (intEndFlag + 8 < strPreviewCode.Length) Then

                strPreviewCode = strPreviewCode.Substring(intEndFlag + 8)
                intStartFlag = strPreviewCode.IndexOf("<Image>")
                intEndFlag = strPreviewCode.IndexOf("</Image>")

            Else
                If intEndFlag + 8 = strPreviewCode.Length Then
                    strPreviewCode = ""
                    intStartFlag = -1
                Else
                    intStartFlag = -1
                End If

            End If

        End While
        intStartFlag = strPreviewCode.IndexOf("<Image>")
        If (strPreviewCode <> "" And intStartFlag = -1) Then
            ParseCode3 += strPreviewCode
        End If
    End Function

    ''' <summary>
    ''' 创建人：张荣江
    ''' 时间：2021-11-02
    ''' 方法描述：检测项内容生成html格式
    ''' </summary>
    ''' <param name="strPreviewCode"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function ParseCode2(ByVal strPreviewCode As String, ByVal url As String) As String
        Dim strPreviewCodeTemp, strHtmlStripped, strTemp, strCodeTemp As String
        Dim strFileDoc, strFileTemp, strFilePath, strFileName, strHtml As String
        Dim intStartFlag, intEndFlag, intFlag1, intFlag2, intCodeStartFlag, intCodeEndFlag As Integer
        Dim strImageIndex As String
        Dim objBmp As Bitmap
        Dim strLeft, strUp, strDown, strRight As String
        ParseCode2 = ""

        strPreviewCode = strPreviewCode.Trim().Replace("\r\n\t\t", "")
        strPreviewCode = strPreviewCode.Trim().Replace("<P>", "")
        strPreviewCode = strPreviewCode.Trim().Replace("</P>", "")
        'strPreviewCode = strPreviewCode.Replace("\"", "'")

        intStartFlag = strPreviewCode.IndexOf("<Image>")
        intEndFlag = strPreviewCode.IndexOf("</Image>")
        strHtmlStripped = ""
        strHtml = ""

        'strFileTemp = strMapPath + @"\Images\";


        Dim strImagePath As String
        strImagePath = ConfigurationSettings.AppSettings("ImageGetPath")
        strFileTemp = strImagePath

        Dim oDraw As New clsDrawImage
        Dim dtImage As New DataTable

        While (intStartFlag > -1)

            If (intStartFlag > 0) Then

                '将纯文本输出
                strHtml = strPreviewCode.Substring(0, intStartFlag)
                strPreviewCode = strPreviewCode.Substring(intStartFlag)
                ' ftbFinalHtml.Text += strHtml
                ParseCode2 += strHtml
                intStartFlag = strPreviewCode.IndexOf("<Image>")
                intEndFlag = strPreviewCode.IndexOf("</Image>")
                Continue While

            Else

                strHtml = ""
                strPreviewCodeTemp = strPreviewCode.Substring(intStartFlag + 7, intEndFlag - intStartFlag - 7)
                intCodeStartFlag = strPreviewCodeTemp.IndexOf("<&70><+>")
                intCodeEndFlag = strPreviewCodeTemp.IndexOf("<+><&90>")

                If (intCodeStartFlag > -1) Then

                    '代码为行位公差时
                    strCodeTemp = strPreviewCodeTemp.Replace("<&70><+>", "")
                    strCodeTemp = strCodeTemp.Replace("<+><&90>", "")

                    intFlag1 = strCodeTemp.IndexOf("<")
                    intFlag2 = strCodeTemp.IndexOf(">")
                    While (intFlag1 > -1)

                        strTemp = strCodeTemp.Substring(intFlag1, intFlag2 - intFlag1 + 1)
                        strCodeTemp = strCodeTemp.Replace(strTemp, "")
                        intFlag1 = strCodeTemp.IndexOf("<")
                        intFlag2 = strCodeTemp.IndexOf(">")
                    End While

                    strHtmlStripped = strCodeTemp
                    'strPreviewCodeTemp = strPreviewCode;
                    strPreviewCodeTemp = strPreviewCodeTemp.Replace("<Image>", "")
                    strPreviewCodeTemp = strPreviewCodeTemp.Replace("</Image>", "")

                    objBmp = oDraw.DrawShapeTolerance(strPreviewCodeTemp, strHtmlStripped, strFileTemp)

                Else

                    intCodeStartFlag = strPreviewCodeTemp.IndexOf("<T")
                    intCodeEndFlag = strPreviewCodeTemp.IndexOf("!")
                    If (intCodeStartFlag > -1) Then

                        '代码为上下标公差

                        strLeft = strPreviewCodeTemp.Substring(0, intCodeStartFlag)
                        strUp = strPreviewCodeTemp.Substring(intCodeStartFlag + 2, intCodeEndFlag - intCodeStartFlag - 2)
                        strDown = strPreviewCodeTemp.Substring(intCodeEndFlag + 1, strPreviewCodeTemp.Length - intCodeEndFlag - 2)
                        objBmp = oDraw.DrawTolerance(strLeft, strUp, strDown)

                    Else

                        intCodeStartFlag = strPreviewCodeTemp.IndexOf("<H>")
                        intCodeEndFlag = strPreviewCodeTemp.IndexOf("</H>")
                        If (intCodeStartFlag > -1) Then

                            '代码为只有上公差
                            strLeft = strPreviewCodeTemp.Substring(0, intCodeStartFlag)
                            strUp = strPreviewCodeTemp.Substring(intCodeStartFlag + 3, intCodeEndFlag - intCodeStartFlag - 3)
                            strDown = ""
                            objBmp = oDraw.DrawTolerance(strLeft, strUp, strDown)

                        Else

                            intCodeStartFlag = strPreviewCodeTemp.IndexOf("<L>")
                            intCodeEndFlag = strPreviewCodeTemp.IndexOf("</L>")
                            If (intCodeStartFlag > -1) Then

                                '代码为只有下公差
                                strLeft = strPreviewCodeTemp.Substring(0, intCodeStartFlag)
                                strUp = ""
                                strDown = strPreviewCodeTemp.Substring(intCodeStartFlag + 3, intCodeEndFlag - intCodeStartFlag - 3)
                                objBmp = oDraw.DrawTolerance(strLeft, strUp, strDown)

                            Else

                                intCodeStartFlag = strPreviewCodeTemp.IndexOf("<√>")
                                intCodeEndFlag = strPreviewCodeTemp.IndexOf("</√>")
                                If (intCodeStartFlag > -1) Then

                                    '代码为指定加工方法时
                                    strLeft = strPreviewCodeTemp.Substring(intCodeStartFlag + 3, intCodeEndFlag - intCodeStartFlag - 3)

                                    strRight = strPreviewCodeTemp.Substring(intCodeEndFlag + 4)
                                    objBmp = oDraw.DrawRoughness(strLeft, strRight)

                                Else

                                    intCodeStartFlag = strPreviewCodeTemp.IndexOf("<R>")
                                    intCodeEndFlag = strPreviewCodeTemp.IndexOf("</R>")
                                    If (intCodeStartFlag > -1) Then

                                        '代码为去除材料时
                                        strLeft = strPreviewCodeTemp.Substring(intCodeStartFlag + 3, intCodeEndFlag - intCodeStartFlag - 3)

                                        strRight = ""
                                        objBmp = oDraw.DrawRoughness(strLeft, strRight)

                                    Else

                                        intCodeStartFlag = strPreviewCodeTemp.IndexOf("<Q>")
                                        intCodeEndFlag = strPreviewCodeTemp.IndexOf("</Q>")
                                        If (intCodeStartFlag > -1) Then

                                            '   代码为不去除材料时()
                                            strLeft = ""

                                            strRight = ""
                                            objBmp = oDraw.DrawRoughness(strLeft, strRight)

                                        Else
                                            'objBmp = Nothing
                                            objBmp = oDraw.DrawSpecImgOrString(strPreviewCodeTemp, strFileTemp) 'add:Wangjh 20230301,注释上一句
                                        End If
                                    End If
                                End If
                            End If
                        End If

                    End If
                End If

                '保存
                If IsNothing(objBmp) = False Then


                    'Dim strImageTempPath As String
                    'strImageTempPath = ConfigurationSettings.AppSettings("ImageTempSavePath")
                    strFileDoc = ConfigurationSettings.AppSettings("UsingCheckImageSavePath")
                    If (System.IO.Directory.Exists(strFileDoc) = False) Then
                        System.IO.Directory.CreateDirectory(strFileDoc)
                    End If

                    strImageIndex = System.DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss_fff")
                    strFileName = "ImageTemp-" + strImageIndex + ".png"
                    'strFileName = "ImageTemp-" + intImageIndex + ".png"
                    strFilePath = strFileDoc + strFileName


                    Dim oFile As New System.IO.FileInfo(strFilePath)
                    While (oFile.Exists = True)

                        strImageIndex = strImageIndex + "1"
                        strFileName = "ImageTemp-" + strImageIndex + ".png"
                        strFilePath = strFileDoc + strFileName
                        oFile = New System.IO.FileInfo(strFilePath)
                    End While

                    objBmp.Save(strFilePath)
                    ' Session["intMaxImageIndex"] = intImageIndex;
                    'If IsNothing(Session("dtImage")) = True Then

                    'strHtml = "<img src='../../../ImageTemp/" + strFileName + "'>"
                    strHtml = "<img src=""" + url + "" + strFileName + """>"
                    'ftbFinalHtml.Text += strHtml
                    ParseCode2 += strHtml
                End If

            End If
            If (intEndFlag + 8 < strPreviewCode.Length) Then

                strPreviewCode = strPreviewCode.Substring(intEndFlag + 8)
                intStartFlag = strPreviewCode.IndexOf("<Image>")
                intEndFlag = strPreviewCode.IndexOf("</Image>")

            Else
                If intEndFlag + 8 = strPreviewCode.Length Then
                    strPreviewCode = ""
                    intStartFlag = -1
                Else
                    intStartFlag = -1
                End If

            End If

        End While
        intStartFlag = strPreviewCode.IndexOf("<Image>")
        If (strPreviewCode <> "" And intStartFlag = -1) Then
            ParseCode2 += strPreviewCode
        End If
    End Function

    Public Shared Function ParseCode(ByVal strPreviewCode As String, ByVal srcUrl As String) As String

        Dim strPreviewCodeTemp, strHtmlStripped, strTemp, strCodeTemp As String
        Dim strFileDoc, strFileTemp, strFilePath, strFileName, strMapPath, strHtml As String
        Dim intStartFlag, intEndFlag, intFlag1, intFlag2, intCodeStartFlag, intCodeEndFlag As Integer
        Dim strImageIndex As String
        Dim objBmp As Bitmap
        Dim strLeft, strUp, strDown, strRight As String
        ParseCode = ""

        strPreviewCode = strPreviewCode.Trim().Replace("\r\n\t\t", "")
        strPreviewCode = strPreviewCode.Trim().Replace("<P>", "")
        strPreviewCode = strPreviewCode.Trim().Replace("</P>", "")
        'strPreviewCode = strPreviewCode.Replace("\"", "'")

        intStartFlag = strPreviewCode.IndexOf("<Image>")
        intEndFlag = strPreviewCode.IndexOf("</Image>")
        strHtmlStripped = ""
        strHtml = ""

        'strFileTemp = strMapPath + @"\Images\";


        Dim strImagePath As String
        strImagePath = ConfigurationSettings.AppSettings("ImageGetPath")
        strFileTemp = strImagePath

        Dim oDraw As New clsDrawImage
        Dim dtImage As New DataTable

        While (intStartFlag > -1)

            If (intStartFlag > 0) Then

                '将纯文本输出
                strHtml = strPreviewCode.Substring(0, intStartFlag)
                strPreviewCode = strPreviewCode.Substring(intStartFlag)
                ' ftbFinalHtml.Text += strHtml
                ParseCode += strHtml
                intStartFlag = strPreviewCode.IndexOf("<Image>")
                intEndFlag = strPreviewCode.IndexOf("</Image>")
                Continue While

            Else

                strHtml = ""
                strPreviewCodeTemp = strPreviewCode.Substring(intStartFlag + 7, intEndFlag - intStartFlag - 7)
                intCodeStartFlag = strPreviewCodeTemp.IndexOf("<&70><+>")
                intCodeEndFlag = strPreviewCodeTemp.IndexOf("<+><&90>")

                If (intCodeStartFlag > -1) Then

                    '代码为行位公差时
                    strCodeTemp = strPreviewCodeTemp.Replace("<&70><+>", "")
                    strCodeTemp = strCodeTemp.Replace("<+><&90>", "")

                    intFlag1 = strCodeTemp.IndexOf("<")
                    intFlag2 = strCodeTemp.IndexOf(">")
                    While (intFlag1 > -1)

                        strTemp = strCodeTemp.Substring(intFlag1, intFlag2 - intFlag1 + 1)
                        strCodeTemp = strCodeTemp.Replace(strTemp, "")
                        intFlag1 = strCodeTemp.IndexOf("<")
                        intFlag2 = strCodeTemp.IndexOf(">")
                    End While

                    strHtmlStripped = strCodeTemp
                    'strPreviewCodeTemp = strPreviewCode;
                    strPreviewCodeTemp = strPreviewCodeTemp.Replace("<Image>", "")
                    strPreviewCodeTemp = strPreviewCodeTemp.Replace("</Image>", "")

                    objBmp = oDraw.DrawShapeTolerance(strPreviewCodeTemp, strHtmlStripped, strFileTemp)

                Else

                    intCodeStartFlag = strPreviewCodeTemp.IndexOf("<T")
                    intCodeEndFlag = strPreviewCodeTemp.IndexOf("!")
                    If (intCodeStartFlag > -1) Then

                        '代码为上下标公差

                        strLeft = strPreviewCodeTemp.Substring(0, intCodeStartFlag)
                        strUp = strPreviewCodeTemp.Substring(intCodeStartFlag + 2, intCodeEndFlag - intCodeStartFlag - 2)
                        strDown = strPreviewCodeTemp.Substring(intCodeEndFlag + 1, strPreviewCodeTemp.Length - intCodeEndFlag - 2)
                        objBmp = oDraw.DrawTolerance(strLeft, strUp, strDown)

                    Else

                        intCodeStartFlag = strPreviewCodeTemp.IndexOf("<H>")
                        intCodeEndFlag = strPreviewCodeTemp.IndexOf("</H>")
                        If (intCodeStartFlag > -1) Then

                            '代码为只有上公差
                            strLeft = strPreviewCodeTemp.Substring(0, intCodeStartFlag)
                            strUp = strPreviewCodeTemp.Substring(intCodeStartFlag + 3, intCodeEndFlag - intCodeStartFlag - 3)
                            strDown = ""
                            objBmp = oDraw.DrawTolerance(strLeft, strUp, strDown)

                        Else

                            intCodeStartFlag = strPreviewCodeTemp.IndexOf("<L>")
                            intCodeEndFlag = strPreviewCodeTemp.IndexOf("</L>")
                            If (intCodeStartFlag > -1) Then

                                '代码为只有下公差
                                strLeft = strPreviewCodeTemp.Substring(0, intCodeStartFlag)
                                strUp = ""
                                strDown = strPreviewCodeTemp.Substring(intCodeStartFlag + 3, intCodeEndFlag - intCodeStartFlag - 3)
                                objBmp = oDraw.DrawTolerance(strLeft, strUp, strDown)

                            Else

                                intCodeStartFlag = strPreviewCodeTemp.IndexOf("<√>")
                                intCodeEndFlag = strPreviewCodeTemp.IndexOf("</√>")
                                If (intCodeStartFlag > -1) Then

                                    '代码为指定加工方法时
                                    strLeft = strPreviewCodeTemp.Substring(intCodeStartFlag + 3, intCodeEndFlag - intCodeStartFlag - 3)

                                    strRight = strPreviewCodeTemp.Substring(intCodeEndFlag + 4)
                                    objBmp = oDraw.DrawRoughness(strLeft, strRight)

                                Else

                                    intCodeStartFlag = strPreviewCodeTemp.IndexOf("<R>")
                                    intCodeEndFlag = strPreviewCodeTemp.IndexOf("</R>")
                                    If (intCodeStartFlag > -1) Then

                                        '代码为去除材料时
                                        strLeft = strPreviewCodeTemp.Substring(intCodeStartFlag + 3, intCodeEndFlag - intCodeStartFlag - 3)

                                        strRight = ""
                                        objBmp = oDraw.DrawRoughness(strLeft, strRight)

                                    Else

                                        intCodeStartFlag = strPreviewCodeTemp.IndexOf("<Q>")
                                        intCodeEndFlag = strPreviewCodeTemp.IndexOf("</Q>")
                                        If (intCodeStartFlag > -1) Then

                                            '   代码为不去除材料时()
                                            strLeft = ""

                                            strRight = ""
                                            objBmp = oDraw.DrawRoughness(strLeft, strRight)

                                        Else

                                            'objBmp = Nothing
                                            objBmp = oDraw.DrawSpecImgOrString(strPreviewCodeTemp, strFileTemp) 'add:Wangjh 20230301,注释上一句
                                        End If
                                    End If
                                End If
                            End If
                        End If

                    End If
                End If

                '保存
                If IsNothing(objBmp) = False Then


                    'Dim strImageTempPath As String
                    'strImageTempPath = ConfigurationSettings.AppSettings("ImageTempSavePath")
                    strFileDoc = ConfigurationSettings.AppSettings("ImageTempSavePath")

                    strImageIndex = System.DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss_fff")
                    strFileName = "ImageTemp-" + strImageIndex + ".png"
                    'strFileName = "ImageTemp-" + intImageIndex + ".png"
                    strFilePath = strFileDoc + strFileName


                    Dim oFile As New System.IO.FileInfo(strFilePath)
                    While (oFile.Exists = True)

                        strImageIndex = strImageIndex + "1"
                        strFileName = "ImageTemp-" + strImageIndex + ".png"
                        strFilePath = strFileDoc + strFileName
                        oFile = New System.IO.FileInfo(strFilePath)
                    End While

                    objBmp.Save(strFilePath)
                    ' Session["intMaxImageIndex"] = intImageIndex;
                    'If IsNothing(Session("dtImage")) = True Then

                    'strHtml = "<img src='../../../ImageTemp/" + strFileName + "'>"
                    strHtml = "<img src='" + srcUrl + strFileName + "'>"
                    'ftbFinalHtml.Text += strHtml
                    ParseCode += strHtml
                End If

            End If
            If (intEndFlag + 8 < strPreviewCode.Length) Then

                strPreviewCode = strPreviewCode.Substring(intEndFlag + 8)
                intStartFlag = strPreviewCode.IndexOf("<Image>")
                intEndFlag = strPreviewCode.IndexOf("</Image>")

            Else
                If intEndFlag + 8 = strPreviewCode.Length Then
                    strPreviewCode = ""
                    intStartFlag = -1
                Else
                    intStartFlag = -1
                End If

            End If

        End While
        intStartFlag = strPreviewCode.IndexOf("<Image>")
        If (strPreviewCode <> "" And intStartFlag = -1) Then
            ParseCode += strPreviewCode
        End If

    End Function

    Public Shared Function ParseCode(ByVal strPreviewCode As String, ByVal strPagePath As String, ByRef dtImageAllInfo As DataTable) As String

        Dim strPreviewCodeTemp, strHtmlStripped, strTemp, strCodeTemp As String
        Dim strFileDoc, strFileTemp, strFilePath, strFileName, strMapPath, strHtml As String
        Dim intStartFlag, intEndFlag, intFlag1, intFlag2, intCodeStartFlag, intCodeEndFlag As Integer
        Dim strImageIndex As String
        Dim objBmp As Bitmap
        Dim strLeft, strUp, strDown, strRight As String
        ParseCode = ""

        strPreviewCode = strPreviewCode.Trim().Replace("\r\n\t\t", "")
        strPreviewCode = strPreviewCode.Trim().Replace("<P>", "")
        strPreviewCode = strPreviewCode.Trim().Replace("</P>", "")
        'strPreviewCode = strPreviewCode.Replace("\"", "'")

        intStartFlag = strPreviewCode.IndexOf("<Image>")
        intEndFlag = strPreviewCode.IndexOf("</Image>")
        strHtmlStripped = ""
        strHtml = ""

        'strFileTemp = strMapPath + @"\Images\";


        Dim strImagePath As String
        strImagePath = ConfigurationSettings.AppSettings("ImageGetPath")
        strFileTemp = strImagePath

        Dim oDraw As New clsDrawImage
        Dim dtImage As New DataTable

        While (intStartFlag > -1)

            If (intStartFlag > 0) Then

                '将纯文本输出
                strHtml = strPreviewCode.Substring(0, intStartFlag)
                strPreviewCode = strPreviewCode.Substring(intStartFlag)
                ' ftbFinalHtml.Text += strHtml
                ParseCode += strHtml
                intStartFlag = strPreviewCode.IndexOf("<Image>")
                intEndFlag = strPreviewCode.IndexOf("</Image>")
                Continue While

            Else

                strHtml = ""
                strPreviewCodeTemp = strPreviewCode.Substring(intStartFlag + 7, intEndFlag - intStartFlag - 7)
                intCodeStartFlag = strPreviewCodeTemp.IndexOf("<&70><+>")
                intCodeEndFlag = strPreviewCodeTemp.IndexOf("<+><&90>")

                If (intCodeStartFlag > -1) Then

                    '代码为行位公差时
                    strCodeTemp = strPreviewCodeTemp.Replace("<&70><+>", "")
                    strCodeTemp = strCodeTemp.Replace("<+><&90>", "")

                    intFlag1 = strCodeTemp.IndexOf("<")
                    intFlag2 = strCodeTemp.IndexOf(">")
                    While (intFlag1 > -1)

                        strTemp = strCodeTemp.Substring(intFlag1, intFlag2 - intFlag1 + 1)
                        strCodeTemp = strCodeTemp.Replace(strTemp, "")
                        intFlag1 = strCodeTemp.IndexOf("<")
                        intFlag2 = strCodeTemp.IndexOf(">")
                    End While

                    strHtmlStripped = strCodeTemp
                    'strPreviewCodeTemp = strPreviewCode;
                    strPreviewCodeTemp = strPreviewCodeTemp.Replace("<Image>", "")
                    strPreviewCodeTemp = strPreviewCodeTemp.Replace("</Image>", "")

                    objBmp = oDraw.DrawShapeTolerance(strPreviewCodeTemp, strHtmlStripped, strFileTemp)

                Else

                    intCodeStartFlag = strPreviewCodeTemp.IndexOf("<T")
                    intCodeEndFlag = strPreviewCodeTemp.IndexOf("!")
                    If (intCodeStartFlag > -1) Then

                        '代码为上下标公差

                        strLeft = strPreviewCodeTemp.Substring(0, intCodeStartFlag)
                        strUp = strPreviewCodeTemp.Substring(intCodeStartFlag + 2, intCodeEndFlag - intCodeStartFlag - 2)
                        strDown = strPreviewCodeTemp.Substring(intCodeEndFlag + 1, strPreviewCodeTemp.Length - intCodeEndFlag - 2)
                        objBmp = oDraw.DrawTolerance(strLeft, strUp, strDown)

                    Else

                        intCodeStartFlag = strPreviewCodeTemp.IndexOf("<H>")
                        intCodeEndFlag = strPreviewCodeTemp.IndexOf("</H>")
                        If (intCodeStartFlag > -1) Then

                            '代码为只有上公差
                            strLeft = strPreviewCodeTemp.Substring(0, intCodeStartFlag)
                            strUp = strPreviewCodeTemp.Substring(intCodeStartFlag + 3, intCodeEndFlag - intCodeStartFlag - 3)
                            strDown = ""
                            objBmp = oDraw.DrawTolerance(strLeft, strUp, strDown)

                        Else

                            intCodeStartFlag = strPreviewCodeTemp.IndexOf("<L>")
                            intCodeEndFlag = strPreviewCodeTemp.IndexOf("</L>")
                            If (intCodeStartFlag > -1) Then

                                '代码为只有下公差
                                strLeft = strPreviewCodeTemp.Substring(0, intCodeStartFlag)
                                strUp = ""
                                strDown = strPreviewCodeTemp.Substring(intCodeStartFlag + 3, intCodeEndFlag - intCodeStartFlag - 3)
                                objBmp = oDraw.DrawTolerance(strLeft, strUp, strDown)

                            Else

                                intCodeStartFlag = strPreviewCodeTemp.IndexOf("<√>")
                                intCodeEndFlag = strPreviewCodeTemp.IndexOf("</√>")
                                If (intCodeStartFlag > -1) Then

                                    '代码为指定加工方法时
                                    strLeft = strPreviewCodeTemp.Substring(intCodeStartFlag + 3, intCodeEndFlag - intCodeStartFlag - 3)

                                    strRight = strPreviewCodeTemp.Substring(intCodeEndFlag + 4)
                                    objBmp = oDraw.DrawRoughness(strLeft, strRight)

                                Else

                                    intCodeStartFlag = strPreviewCodeTemp.IndexOf("<R>")
                                    intCodeEndFlag = strPreviewCodeTemp.IndexOf("</R>")
                                    If (intCodeStartFlag > -1) Then

                                        '代码为去除材料时
                                        strLeft = strPreviewCodeTemp.Substring(intCodeStartFlag + 3, intCodeEndFlag - intCodeStartFlag - 3)

                                        strRight = ""
                                        objBmp = oDraw.DrawRoughness(strLeft, strRight)

                                    Else

                                        intCodeStartFlag = strPreviewCodeTemp.IndexOf("<Q>")
                                        intCodeEndFlag = strPreviewCodeTemp.IndexOf("</Q>")
                                        If (intCodeStartFlag > -1) Then

                                            '   代码为不去除材料时()
                                            strLeft = ""

                                            strRight = ""
                                            objBmp = oDraw.DrawRoughness(strLeft, strRight)

                                        Else
                                            'objBmp = Nothing
                                            objBmp = oDraw.DrawSpecImgOrString(strPreviewCodeTemp, strFileTemp) 'add:Wangjh 20230301,注释上一句
                                        End If
                                    End If
                                End If
                            End If
                        End If

                    End If
                End If

                '保存
                If IsNothing(objBmp) = False Then


                    'Dim strImageTempPath As String
                    'strImageTempPath = ConfigurationSettings.AppSettings("ImageTempSavePath")
                    strFileDoc = ConfigurationSettings.AppSettings("ImageTempSavePath")

                    strImageIndex = System.DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss_fff")
                    strFileName = "ImageTemp-" + strImageIndex + ".png"
                    'strFileName = "ImageTemp-" + intImageIndex + ".png"
                    strFilePath = strFileDoc + strFileName


                    Dim oFile As New System.IO.FileInfo(strFilePath)
                    While (oFile.Exists = True)

                        strImageIndex = strImageIndex + "1"
                        strFileName = "ImageTemp-" + strImageIndex + ".png"
                        strFilePath = strFileDoc + strFileName
                        oFile = New System.IO.FileInfo(strFilePath)
                    End While

                    objBmp.Save(strFilePath)
                    ' Session["intMaxImageIndex"] = intImageIndex;
                    'If IsNothing(Session("dtImage")) = True Then

                    If IsNothing(dtImageAllInfo) = True Then


                        'Dim dtImage As New DataTable
                        dtImage.Columns.Add("FileName")
                        dtImage.Columns.Add("HtmlCode")
                        'Session("dtImage") = dtImage
                        dtImageAllInfo = dtImage.Copy
                    Else

                        'dtImage = Session("dtImage")
                        dtImage = dtImageAllInfo
                    End If

                    Dim dr As DataRow
                    dr = dtImage.NewRow()
                    dr(0) = strFileName
                    dr(1) = "<Image>" + strPreviewCodeTemp + "</Image>"
                    dtImage.Rows.Add(dr)
                    'Session("dtImage") = dtImage
                    dtImageAllInfo = dtImage.Copy

                    Dim strImagePathDisp As String = ""
                    Dim strPagePathTemp As String = ""
                    strPagePathTemp = strPagePath.Substring(0, strPagePath.LastIndexOf("/"))

                    Dim intCount As Integer
                    intCount = strPagePathTemp.Length - strPagePathTemp.Replace("/", "").Length
                    For j = 0 To intCount - 2
                        strImagePathDisp = "../" & strImagePathDisp
                    Next
                    strImagePathDisp = strImagePathDisp & "ImageTemp/"

                    strFileDoc = strImagePathDisp 'ConfigurationSettings.AppSettings("ImageTempPath")
                    'strHtml = "<img src='../../../ImageTemp/" + strFileName + "'>"
                    strHtml = "<img src='" + strFileDoc + strFileName + "'>"
                    'ftbFinalHtml.Text += strHtml
                    ParseCode += strHtml
                End If

            End If
            If (intEndFlag + 8 < strPreviewCode.Length) Then

                strPreviewCode = strPreviewCode.Substring(intEndFlag + 8)
                intStartFlag = strPreviewCode.IndexOf("<Image>")
                intEndFlag = strPreviewCode.IndexOf("</Image>")

            Else
                If intEndFlag + 8 = strPreviewCode.Length Then
                    strPreviewCode = ""
                    intStartFlag = -1
                Else
                    intStartFlag = -1
                End If

            End If

        End While
        intStartFlag = strPreviewCode.IndexOf("<Image>")
        If (strPreviewCode <> "" And intStartFlag = -1) Then
            ParseCode += strPreviewCode
        End If
        If IsNothing(dtImageAllInfo) = True Then
            dtImageAllInfo = New DataTable
            dtImageAllInfo.Columns.Add("FileName")
            dtImageAllInfo.Columns.Add("HtmlCode")
        End If
    End Function
#End Region

#Region "分页方法"

    Public Shared Function FormatNavString(ByVal reqUrl As String, ByVal reqPage As Integer, ByVal totalRec As Integer, ByVal pageSize As Integer) As String

        If pageSize <= 0 Then
            Throw New ArgumentException("参数 pageSize 必须大于 0.", "pageSize")
        End If

        Dim totalPage As Integer = Convert.ToInt32(System.Math.Ceiling(Convert.ToDouble(totalRec) / pageSize))

        Dim sb As StringBuilder = New StringBuilder()

        If totalPage > 0 Then
            sb.AppendFormat("页次：{0}/{1}页 每页{2} 记录数{3}&nbsp; 分页：", reqPage, totalPage, pageSize, totalRec)
            sb.Append(String.Format("<a  href='{0}'>|&lt;</a>&nbsp;", String.Format(reqUrl, 1)))
        End If



        If totalPage <= 10 Then

            For i As Integer = 1 To totalPage

                If i <> reqPage Then
                    sb.Append(String.Format("<a  href='{0}'>{1}</a>&nbsp;", String.Format(reqUrl, i), i))
                Else
                    sb.Append(String.Format("<font color='red'>{0}</font>&nbsp;", i))
                End If
            Next

        Else

            If reqPage <= 5 Then

                For i As Integer = 1 To 10

                    If i <> reqPage Then

                        sb.Append(String.Format("<a  href='{0}'>{1}</a>&nbsp;", String.Format(reqUrl, i), i))
                    Else

                        sb.Append(String.Format("<a  href='{0}'><font color='red'>{1}</font></a>&nbsp;", String.Format(reqUrl, i), i))
                    End If


                Next
                sb.Append("&nbsp;...")


            Else

                sb.Append("&nbsp;...")
                If reqPage >= totalPage - 5 Then


                    For i As Integer = totalPage - 9 To totalPage

                        If i <> reqPage Then

                            sb.Append(String.Format("<a  href='{0}'>{1}</a>&nbsp;", String.Format(reqUrl, i), i))
                        Else
                            sb.Append(String.Format("<a  href='{0}'><font color='red'>{1}</font></a>&nbsp;", String.Format(reqUrl, i), i))
                        End If
                    Next

                Else

                    For i As Integer = reqPage - 5 To reqPage + 5

                        If i <> reqPage Then

                            sb.Append(String.Format("<a href='{0}'>{1}</a>&nbsp;", String.Format(reqUrl, i), i))
                        Else

                            sb.Append(String.Format("<a  href='{0}'><font color='red'>{1}</font></a>&nbsp;", String.Format(reqUrl, i), i))
                        End If
                    Next
                    sb.Append("&nbsp;...")
                End If
            End If
        End If
        sb.Append(String.Format("<a  href='{0}'>&gt;|</a>", String.Format(reqUrl, totalPage)))
        FormatNavString = sb.ToString().Trim()

    End Function

#End Region
End Class
